//package com.kuang.controller.order;
//
//import com.kuang.common.anno.IgnoreToken;
//import com.kuang.common.base.BaseController;
//import com.kuang.common.err.ValidationException;
//import com.kuang.entity.Order;
//import com.kuang.generator.redis.IGeneratorIdService;
//import com.kuang.generator.snow.SnowFlakeShortUrl;
//import com.kuang.service.OrderService;
//import org.apache.commons.lang3.StringUtils;
//import org.apache.curator.framework.CuratorFramework;
//import org.apache.curator.framework.recipes.locks.InterProcessMutex;
//import org.redisson.api.RLock;
//import org.redisson.api.RedissonClient;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.data.redis.core.RedisTemplate;
//import org.springframework.web.bind.annotation.PostMapping;
//import org.springframework.web.bind.annotation.RequestBody;
//import org.springframework.web.bind.annotation.RestController;
//
//import javax.servlet.http.HttpSession;
//import java.math.BigDecimal;
//import java.util.Objects;
//import java.util.concurrent.TimeUnit;
//
///**
// * @author 飞哥
// * @Title: 学相伴出品
// * @Description: 我们有一个学习网站：https://www.kuangstudy.com
// * @date 2021/9/3 14:06
// */
//@RestController
//public class OrderController extends BaseController {
//
//    private final static Logger log = LoggerFactory.getLogger(OrderController.class);
//
//    @Autowired
//    private OrderService orderService;
//    @Autowired
//    private IGeneratorIdService generatorIdService;
//    @Autowired
//    private CuratorFramework client;
//    @Autowired
//    private RedisTemplate redisTemplate;
//    @Autowired
//    private RedissonClient redissonClient;
//
//    @PostMapping("/saveorder")
//    @IgnoreToken
//    public Order saveOrder(@RequestBody Order order) {
//        if (StringUtils.isEmpty(order.getToken())) {
//            throw new ValidationException(234, "Token不允许为空!!!");
//        }
//        String tokenKey = "o-token-" + order.getToken();
//        RLock rLock = redissonClient.getLock(tokenKey);
//        try {
//            rLock.lock(5, TimeUnit.SECONDS);
//            String orderToken = (String) redisTemplate.opsForValue().get(tokenKey);
//            if (StringUtils.isEmpty(orderToken)) {
//                throw new ValidationException(234, "Token不允许为空!!!");
//            }
//
//            if (!Objects.equals(order.getToken(), orderToken)) {
//                throw new ValidationException(234, "Token不合法!!!");
//            }
//
//            // 删除redis中的token
//            redisTemplate.delete(tokenKey);
//        } catch (Exception ex) {
//            throw new ValidationException(234, "Token不允许为空!!!");
//        } finally {
//            rLock.unlock();
//        }
//
//        log.info("开始保存订单了....");
//        InterProcessMutex lock = new InterProcessMutex(client, "/order/" + order.getToken());
//        try {
//            boolean isLock = lock.acquire(30, TimeUnit.SECONDS);
//            if (isLock) {
//                log.info("我获取了锁!!!!");
//                order.setUserId(1L);
//                long orderNumber = new SnowFlakeShortUrl(1, 1).nextId();
//                String order_bitnumber = generatorIdService.getSequence("order_bitnumber");
//                order.setOrderNumber(orderNumber + "");
//                order.setBitNumber(order_bitnumber);
//                order.setProductId(1L);
//                order.setUsername("yykk");
//                order.setAvatar("a.jpg");
//                order.setPaymethod(1);
//                order.setPlatform(1);
//                order.setIp("127.0.0.1");
//                order.setProductPrice(new BigDecimal("12.3"));
//                order.setProductTitle("小米手机");
//                order.setProductDesc("太贵了...");
//                order.setProductImg("xiaomi.jpg");
//                order.setIpaddr("本机");
//                orderService.saveOrder(order);
//                log.info("保存订单成功!!!!,{}", order);
//                return order;
//            }
//            throw new ValidationException(302, "你慢一点点");
//        } catch (Exception ex) {
//            throw new ValidationException(302, "你慢一点点");
//        } finally {
//            try {
//                log.info("我释放了锁!!!!");
//                //lock.release();
//            } catch (Exception ex) {
//                ex.printStackTrace();
//            }
//        }
//    }
//
//
////    @PostMapping("/saveorder")
////    @IgnoreToken
////    public Order saveOrder(@RequestBody Order order, HttpSession session ) {
////        log.info("开始保存订单了....");
////        InterProcessMutex lock = new InterProcessMutex(client, "/order/" + order.getUserId() + "/" + order.getProductId());
////        try {
////            boolean isLock = lock.acquire(30, TimeUnit.SECONDS);
////            if (isLock) {
////                log.info("我获取了锁!!!!");
////                order.setUserId(1L);
////                long orderNumber = new SnowFlakeShortUrl(1, 1).nextId();
////                String order_bitnumber = generatorIdService.getSequence("order_bitnumber");
////                order.setOrderNumber(orderNumber + "");
////                order.setBitNumber(order_bitnumber);
////                order.setProductId(1L);
////                order.setUsername("yykk");
////                order.setAvatar("a.jpg");
////                order.setPaymethod(1);
////                order.setPlatform(1);
////                order.setIp("127.0.0.1");
////                order.setProductPrice(new BigDecimal("12.3"));
////                order.setProductTitle("小米手机");
////                order.setProductDesc("太贵了...");
////                order.setProductImg("xiaomi.jpg");
////                order.setIpaddr("本机");
////                orderService.saveOrder(order);
////                log.info("保存订单成功!!!!,{}", order);
////                return order;
////            }
////            throw new ValidationException(302, "你慢一点点");
////        } catch (Exception ex) {
////            throw new ValidationException(302, "你慢一点点");
////        } finally {
////            try {
////                log.info("我释放了锁!!!!");
////                //lock.release();
////            } catch (Exception ex) {
////                ex.printStackTrace();
////            }
////        }
////    }
//
//
////    @PostMapping("/saveorder")
////    @IgnoreToken
////    public Order saveOrder(@RequestBody Order order) {
////        order.setUserId(1L);
////        long orderNumber = new SnowFlakeShortUrl(1, 1).nextId();
////        String order_bitnumber = generatorIdService.getSequence("order_bitnumber");
////        order.setOrderNumber(orderNumber + "");
////        order.setBitNumber(order_bitnumber);
////        order.setProductId(1L);
////        order.setUsername("yykk");
////        order.setAvatar("a.jpg");
////        order.setPaymethod(1);
////        order.setPlatform(1);
////        order.setIp("127.0.0.1");
////        order.setProductPrice(new BigDecimal("12.3"));
////        order.setProductTitle("小米手机");
////        order.setProductDesc("太贵了...");
////        order.setProductImg("xiaomi.jpg");
////        order.setIpaddr("本机");
////        orderService.saveOrder(order);
////        return order;
////    }
//}
